<?php
/**
 * @title 698. 划分为k个相等的子集
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }


class Solution {

    /**
     * @param Integer[] $nums
     * @param Integer $k
     * @return Boolean
     */
    function canPartitionKSubsets($nums, $k) {
        /**
         * @since 2022-09-20 总和
         */
        $sum = array_sum($nums);

        /**
         * @since 2022-09-20 是否可以平均分
         */
        if($sum % $k != 0){
            /**
             * @return
             */
            return false;
        } else {
            $avg = $sum/$k;
        }

        /**
         * @since 2022-09-20 最大值, 预剪枝是否有解
         */
        $max = max($nums);
        if($max > $avg){
            return false;
        } else {}
        // echo "k={$k}, avg={$avg}, nums=[", implode(", ", $nums) ,"]", PHP_EOL;die();

        /**
         * @since 2022-09-20 逐个尝试，是否有解
         */
        $result = self::find(0, $nums, $k, $avg);

        /**
         * @return
         */
        return $result;
    }

    /**
     * @title 是否有解
     * @author start2004
     * @since 2022-09-20 Created
     *
     * @param int $sum
     * @param array $nums
     * @param int $k
     * @param int $targetSum
     * @return boolean
     */
    function find($sum = 0, $nums = [], $k = 0, $targetSum = 0){
        // echo "sum={$sum}, k={$k}, nums=", implode(",", $nums), PHP_EOL;
        if($k == 0){
            return true;
        } else {}

        /**
         * @since 2022-09-20 逐位尝试
         */
        foreach ($nums as $key=>$num){
            if($sum+$num == $targetSum){
                /**
                 * @since 2022-09-20 凑齐targetSum
                 */
                unset($nums[$key]);

                /**
                 * @since 2022-09-20 继续找，无解
                 */
                if(self::find(0, $nums, $k-1, $targetSum) === true){
                    return true;
                } else {
                    return false;
                }
            } elseif($sum+$num > $targetSum) {
                /**
                 * @since 2022-09-20 无解
                 */
            } else {
                /**
                 * @since 2022-09-20 sum+num < targetSum，尝试
                 */
                unset($nums[$key]);

                /**
                 * @since 2022-09-20 继续凑targetSum
                 */
                if(self::find($sum+$num, $nums, $k, $targetSum) === true){
                    return true;
                } else {
                    /**
                     * @since 2022-09-20 换个其他方式
                     */
                    $nums[$key] = $num;
                }
            }
        }

        /**
         * @return
         */
        return false;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202209/2022.09.20.php
 */
$datas = [
    [[4, 3, 2, 3, 5, 2, 1], 4], // true
    [[1,2,3,4], 3], // false
    [[18,20,39,73,96,99,101,111,114,190,207,295,471,649,700,1037], 4], //
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->canPartitionKSubsets(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
